package com.jackiechan.hello.day02;

import java.util.Scanner;

/**
 * 要求用户输入一个字符串，然后若该字符串是一个整数，则转换为整数后输出乘以10后的结果
 * 若是小数，则转换为一个小数后输出乘以5后的结果，若不是数字则输出"不是数字"
 * 需要使用正则表达式进行判断。
 * @author Xiloer
 *
 */
public class Test01 {
    public static void main(String[] args) {
        while (true){
            System.out.print("请输入字符串:");
            String string = new Scanner(System.in).nextLine();
//            show(string);
            if (string.equals("q")) break;
            regular(string);
        }
    }

    private static void regular(String string) {
        StringBuilder sb = new StringBuilder(string);
        if (string.matches("[/+/-]?[0-9]+\\.?")){
            System.out.print("整数： ");
            Integer integer;
            if (sb.indexOf(".")==sb.length()-1){
                integer = new Integer(sb.substring(0,sb.indexOf(".")));
            }else {
                integer = new Integer(string);
            }
            System.out.println(integer*10);
        }else if (string.matches("[/+/-]?[0-9]*\\.[0-9]+")){
            System.out.print("小数： ");
            Integer integer;
            Integer decimal;
            double num = 0;
            int index = sb.indexOf(".");
            if (index==0){
                num = new Double(string);
            } else if (string.matches("^[/+/-]/.[0-9]*")&&index==1){
                System.out.println("+.5");
            } else {
//                integer = new Integer(sb.substring(0,index));
//                decimal = new Integer(sb.substring(index+1));
//                num = integer+decimal*Math.pow(0.1,sb.length()-index-1);
                num = new Double(string);
            }
                System.out.println(num*5);
        }else {
            System.out.println("不是数字");
        }
//        boolean b = string.matches("[1-9]*\\.[1-9]*");
//        System.out.println(b);
    }

    private static void show(String string) {
        StringBuilder sb = new StringBuilder(string);
        try {
            int index = string.trim().indexOf(".");
//            System.out.println(index);
            if (index == -1){
                int integer = new Integer(string);
                System.out.println(integer*10);
            }else if (index == 0){
                int decimal = new Integer(sb.substring(index+1));
                double num = decimal*Math.pow(0.1,sb.length()-1);
                System.out.println(num*5);
            }else if (index == sb.length()-1){
                int integer = new Integer(sb.substring(0,index));
                System.out.println(integer*10);
            }else {
                int integer = new Integer(sb.substring(0,index));
                int decimal = new Integer(sb.substring(index+1));
                double num = integer+decimal*Math.pow(0.1,sb.length()-index-1);
                System.out.println(num*5);
            }
        }catch (NumberFormatException e){
            System.out.println("不是数字");
        }
    }

}
